package org.summer.lawyer.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.summer.lawyer.domain.bo.brokeragerecord.BrokerageRecordBo;
import org.summer.lawyer.domain.bo.brokerageuser.CommissionBo;
import org.summer.lawyer.domain.bo.spuorder.SpuOrderBo;
import org.summer.lawyer.domain.bo.spuorderassign.SpuOrderAssignBo;
import org.summer.lawyer.domain.vo.brokeragerecord.BrokerageRecordVo;
import org.summer.lawyer.domain.vo.spuorder.SpuOrderVo;
import org.summer.lawyer.domain.vo.spuorderassign.SpuOrderAssignVo;
import org.summer.lawyer.service.IBrokerageRecordService;
import org.summer.lawyer.service.IBrokerageUserService;
import org.summer.lawyer.service.ISpuOrderAssignService;
import org.summer.lawyer.service.ISpuOrderService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Slf4j
@Component
public class SpuOrderTask {

    @Resource
    private ISpuOrderService spuOrderService;

    @Resource
    private ISpuOrderAssignService spuOrderAssignService;

    @Resource
    private IBrokerageRecordService brokerageRecordService;

    @Resource
    private IBrokerageUserService brokerageUserService;

    /**
     * 处理订单支付超时
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void processTimeoutOrder() {
        log.info("定时处理支付超时订单{} ", DateUtil.now());
        Date now = DateUtil.offsetMinute(DateUtil.date(), -15);
        Map<String, Object> params = new HashMap<>();
        params.put("now", now);
        SpuOrderBo spuOrderBo = new SpuOrderBo();
        spuOrderBo.setParams(params);
        List<SpuOrderVo> spuOrderVos = spuOrderService.selectTimeoutOrder(spuOrderBo);
        if (CollUtil.isNotEmpty(spuOrderVos)) {
            for (SpuOrderVo spuOrderVo : spuOrderVos) {
                spuOrderService.cancelSpuOrder(spuOrderVo.getOrderId());
            }
        }
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void processAssignOrder() {
        log.info("定时处理超时分派订单{} ", DateUtil.now());
        Date now = DateUtil.date();
        Map<String, Object> params = new HashMap<>();
        params.put("now", now);
        SpuOrderAssignBo orderAssignBo = new SpuOrderAssignBo();
        orderAssignBo.setParams(params);
        List<SpuOrderAssignVo> assignVos = spuOrderAssignService.selectTimeoutSpuOrderAssignList(orderAssignBo);
        if(CollUtil.isNotEmpty(assignVos)){
            for (SpuOrderAssignVo assignVo : assignVos) {
                spuOrderAssignService.cancelAssignOrder(assignVo.getId());
            }
        }
    }

    @Scheduled(cron = "0 0/10 * * * ?")
    public void processWithdraw(){
        log.info("定时处理佣金结算{} ", DateUtil.now());
        BrokerageRecordBo bo = new BrokerageRecordBo();
        bo.setUnfreezeTime(DateUtil.date());
        List<BrokerageRecordVo> brokerageRecordVos = brokerageRecordService.selectCommissionRecord(bo);
        if(CollUtil.isNotEmpty(brokerageRecordVos)){
            for (BrokerageRecordVo brokerageRecordVo : brokerageRecordVos) {
                log.info("[processWithdraw][微信用户({})]开始结算佣金 {} 分", brokerageRecordVo.getWxUserId(), brokerageRecordVo.getAmount());
                CommissionBo commissionBo = new CommissionBo();
                commissionBo.setWxUserId(brokerageRecordVo.getWxUserId());
                commissionBo.setBrokerageRecordId(brokerageRecordVo.getId());
                brokerageUserService.commissionSettlement(commissionBo);
            }
        }
    }

}
